package com.ruoyi.zsyh.controller.applet.store;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 产品Controller
 */
@Controller
@RequestMapping("/applet/zsyh/storeParts")
public class ZsyhStorePartsAppletController extends BaseController
{

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhPlatformPartsService zsyhPlatformPartsService;

    @Autowired
    private IZsyhAgentPartsService zsyhAgentPartsService;

    @Autowired
    private IZsyhStorePartsselfService zsyhStorePartsselfService;

    @Autowired
    private IZsyhStoreAttributeselfService zsyhStoreAttributeselfService;

    @Autowired
    private IZsyhUserAddressService zsyhUserAddressService;

    @Autowired
    private IZsyhStoreOrderService zsyhStoreOrderService;

    @Autowired
    private IZsyhStoreOrderRelService zsyhStoreOrderRelService;


    /**
     * 产品大全，根据热门推荐查询店铺列表
     * @return
     */
    @RequestMapping("/getStoreListByPartsId")
    @ResponseBody
    public AjaxResult getPartsList(String lat, String lon, String partsId, String pageNum, String pageSize){
        int num = Integer.valueOf(pageNum);
        int size = Integer.valueOf(pageSize);
        num = (num - 1) * size;
        List<Map> list = zsyhUserRepairstoreService.selectUserStoreListByPartsId(lat, lon, partsId, num, size);
        return AjaxResult.success("获取成功", list);
    }


    /**
     * 产品大全，根据产品分类 查询店铺列表 搜索
     * @return
     */
    @RequestMapping("/selectUserStoreListByTypeId")
    @ResponseBody
    public AjaxResult selectUserStoreListByTypeId(String partsType, String key, String pageNum, String pageSize){
        int num = Integer.valueOf(pageNum);
        int size = Integer.valueOf(pageSize);
        num = (num - 1) * size;
        List<Map> list = zsyhUserRepairstoreService.selectPartsListByTypeId(partsType, key, num, size);
        return AjaxResult.success("获取成功", list);
    }


    /**
     * 产品大全，获取热门推荐产品详情 平台产品
     * @return
     */
    @RequestMapping("/getPlatformPartsDetails")
    @ResponseBody
    public AjaxResult getPlatformPartsDetails(String partsId){
        ZsyhPlatformParts parts = zsyhPlatformPartsService.selectZsyhPlatformPartsByIdStore("2", partsId);
        parts.setFactoryName("主事丫环配件仓库");
        ZsyhPlatformParts parts1 = new ZsyhPlatformParts();
        parts1.setIsHotPush("1");
        //parts1.setFactoryId(parts.getFactoryId());
        Map map = new HashedMap();
        map.put("beginPos", 0);
        map.put("pageSize", 10);
        parts1.setParams(map);
        parts1.setIsShowWorker("1");
        List<ZsyhPlatformParts> list = zsyhPlatformPartsService.selectZsyhPlatformPartsList(parts1);
        for(ZsyhPlatformParts a:list){
            a.setPartsStoreType("1");
        }
        Map map1 = new HashMap();
        map1.put("parts", parts);
        map1.put("partsList", list);
//
//        //如果通过店铺进入的，查询店铺的全部商品
//        if(EmptyUtils.isNotEmpty(partsStoreId)){
//            ZsyhStorePartsself parts2 = new ZsyhStorePartsself();
//            parts2.setStoreId(partsStoreId);
//            parts2.setParams(map);
//            parts2.setIsShow("1");
//            List<ZsyhStorePartsself> list1 = zsyhStorePartsselfService.selectZsyhStorePartsselfList(parts2);
//            List<ZsyhPlatformParts> list2 = new ArrayList<>();
//            for(ZsyhStorePartsself a:list1){
//                ZsyhPlatformParts parts3 = new ZsyhPlatformParts();
//                parts3.setPartsStoreType("2");
//                parts3.setId(a.getId());
//                parts3.setMinPriceRetail(a.getMinPrice());
//                parts3.setMaxPriceRetail(a.getMaxPrice());
//                parts3.setPartsName(a.getPartsName());
//                parts3.setPartsImages(a.getPartsImages());
//                parts3.setPartsStoreUnit(a.getPartsUnit());
//                list2.add(parts3);
//            }
//            ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreById(partsStoreId);
//            String storeName = store.getStoreName();
//            parts.setFactoryName(storeName);
//            map1 = new HashMap();
//            map1.put("parts", parts);
//            map1.put("partsList", list2);
//        }
//        map1.put("partsStoreId", partsStoreId);

        return AjaxResult.success("获取成功", map1);
    }


    /**
     * 获取会长产品详情
     * @param partsId
     * @return
     */
    @RequestMapping("/getAgentPartsDetails")
    @ResponseBody
    public AjaxResult getAgentPartsDetails(String partsId){
        ZsyhAgentParts parts = zsyhAgentPartsService.selectZsyhAgentPartsById(partsId);
        ZsyhAgentParts parts1 = new ZsyhAgentParts();
        parts1.setAgentId(parts.getAgentId());
        Map map = new HashedMap();
        map.put("beginPos", 0);
        map.put("pageSize", 10);
        parts1.setParams(map);
        parts1.setIsShow("1");
        List<ZsyhAgentParts> list = zsyhAgentPartsService.selectZsyhAgentPartsList(parts1);
        Map map1 = new HashMap();
        map1.put("parts", parts);
        map1.put("partsList", list);
        return AjaxResult.success("获取成功", map1);
    }


    /**
     * 获取店铺的产品详情
     * @param partsId
     * @return
     */
    @RequestMapping("/getStorePartsDetails")
    @ResponseBody
    public AjaxResult getStorePartsDetails(String partsId){
        ZsyhStorePartsself parts = zsyhStorePartsselfService.selectZsyhStorePartsselfById(partsId);
        ZsyhStorePartsself parts1 = new ZsyhStorePartsself();
        parts1.setStoreId(parts.getStoreId());
        Map map = new HashedMap();
        map.put("beginPos", 0);
        map.put("pageSize", 10);
        parts1.setParams(map);
        parts1.setIsShow("1");
        List<ZsyhStorePartsself> list = zsyhStorePartsselfService.selectZsyhStorePartsselfList(parts1);
        Map map1 = new HashMap();
        map1.put("parts", parts);
        map1.put("partsList", list);
        return AjaxResult.success("获取成功", map1);
    }

    /**
     * 获取会长产品列表
     * @return
     */
    @RequestMapping("/getAgentPartsList")
    @ResponseBody
    public AjaxResult getAgentPartsList(String agentId, String pageNum, String pageSize){
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhAgentParts parts = new ZsyhAgentParts();
        parts.setAgentId(agentId);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        parts.setParams(map);
        parts.setIsShow("1");
        List<ZsyhAgentParts> list = zsyhAgentPartsService.selectZsyhAgentPartsList(parts);
        return AjaxResult.success("获取成功", list);
    }


    /**
     * 获取店铺的产品列表
     * @return
     */
    @RequestMapping("/getStorePartsList")
    @ResponseBody
    public AjaxResult getStorePartsList(String storeId, String pageNum, String pageSize){
        ZsyhStorePartsself parts = new ZsyhStorePartsself();
        parts.setStoreId(storeId);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        parts.setParams(map);
        parts.setIsShow("1");
        List<ZsyhStorePartsself> list = zsyhStorePartsselfService.selectZsyhStorePartsselfList(parts);
        return AjaxResult.success("获取成功", list);
    }


    /**
     * 提交预订单  -- 店铺产品
     * @param partsStoreId
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/submitStoreReservation")
    @ResponseBody
    @Transactional
    public AjaxResult submitStoreReservation(String addressId, String partsStoreId, String partsJson){

        ZsyhUserAddress address = zsyhUserAddressService.selectZsyhUserAddressById(addressId);
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(address.getUserId());
        String storeId = store.getId();
        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(partsJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhStoreOrderRel> relList = new ArrayList<>();
        //保存订单信息
        String id = IdWorker.getId();
        Date date = new Date();
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setCreateTime(date);
        order.setPartsStoreId(partsStoreId);
        order.setStoreId(storeId);
        order.setCreateDate(DateUtils.getDate());
        order.setOrderName(address.getAddressName());
        order.setOrderPhone(address.getAddressPhone());
        order.setOrderAddress(address.getAddressPrivinceName() + address.getAddressCityName() + address.getAddressDistrictName() + " " + address.getAddressDetail());
        order.setOrderState("1");//未接单
        List<ZsyhStoreOrder> orderList = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
        if(orderList.size() == 0){
            order.setId(id);
            order.setAgentId(store.getAgentId());
            order.setOrderType("1");
            zsyhStoreOrderService.insertZsyhStoreOrderApplet(order);
        }else{
            order.setId(orderList.get(0).getId());
            zsyhStoreOrderService.updateZsyhStoreOrderApplet(order);
        }

        for(Map<String, Object> a:list){//解析预定产品，并保存
            String attributeId = String.valueOf(a.get("attributeId"));
            String partsId = String.valueOf(a.get("partsId"));
            String count = String.valueOf(a.get("orderCount"));
            BigDecimal price = new BigDecimal(String.valueOf(a.get("orderPrice")));

            ZsyhStoreOrderRel rel = ZsyhStoreOrderRel.getInstance();
            rel.setId(IdWorker.getId());
            rel.setOrderId(order.getId());
            rel.setPartsId(partsId);
            rel.setAttributeId(attributeId);
            rel.setOrderCount(count);
            rel.setOrderPrice(price);
            rel.setCreateTime(date);
            rel.setIsPlatform("3");
            relList.add(rel);
        }
        zsyhStoreOrderRelService.insertZsyhStoreOrderRelBatch(relList);

        return success("操作成功");

    }

    /**
     * 提交预订单  -- 会长产品
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/submitAgentReservation")
    @ResponseBody
    @Transactional
    public AjaxResult submitAgentReservation(String addressId, String partsJson){

        ZsyhUserAddress address = zsyhUserAddressService.selectZsyhUserAddressById(addressId);
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(address.getUserId());
        String storeId = store.getId();
        String agentId = store.getAgentId();
        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(partsJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhStoreOrderRel> relList = new ArrayList<>();
        //保存订单信息
        String id = IdWorker.getId();
        Date date = new Date();
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setCreateTime(date);
        order.setStoreId(storeId);
        order.setCreateDate(DateUtils.getDate());
        order.setOrderName(address.getAddressName());
        order.setOrderPhone(address.getAddressPhone());
        order.setOrderAddress(address.getAddressPrivinceName() + address.getAddressCityName() + address.getAddressDistrictName() + " " + address.getAddressDetail());
        order.setOrderState("1");//未接单
        List<ZsyhStoreOrder> orderList = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
        order.setPartsStoreId("");
        if(orderList.size() == 0){
            order.setId(id);
            order.setAgentId(agentId);
            order.setOrderType("2");
            zsyhStoreOrderService.insertZsyhStoreOrderApplet(order);
        }else{
            order.setId(orderList.get(0).getId());
            zsyhStoreOrderService.updateZsyhStoreOrderApplet(order);
        }

        for(Map<String, Object> a:list){//解析预定产品，并保存
            String attributeId = String.valueOf(a.get("attributeId"));
            String partsId = String.valueOf(a.get("partsId"));
            String count = String.valueOf(a.get("orderCount"));
            BigDecimal price = new BigDecimal(String.valueOf(a.get("orderPrice")));

            ZsyhStoreOrderRel rel = ZsyhStoreOrderRel.getInstance();
            rel.setId(IdWorker.getId());
            rel.setOrderId(order.getId());
            rel.setPartsId(partsId);
            rel.setAttributeId(attributeId);
            rel.setOrderCount(count);
            rel.setOrderPrice(price);
            rel.setCreateTime(date);
            rel.setIsPlatform("2");
            relList.add(rel);
        }
        zsyhStoreOrderRelService.insertZsyhStoreOrderRelBatch(relList);

        return success("操作成功");

    }


    /**
     * 提交预订单  -- 平台产品
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/submitPlatformReservation")
    @ResponseBody
    @Transactional
    public AjaxResult submitPlatformReservation(String userId, String partsJson){

        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String storeId = store.getId();
        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(partsJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhStoreOrderRel> relList = new ArrayList<>();
        //保存订单信息
        String id = IdWorker.getId();
        Date date = new Date();
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setCreateTime(date);
        order.setAgentId(store.getAgentId());
        order.setStoreId(storeId);
        order.setOrderName(store.getStorePerson());
        order.setOrderPhone(store.getStorePhone());
        order.setOrderAddress(store.getStoreProvinceName() + store.getStoreCityName() + store.getStoreDistrictName() + " " + store.getStoreAddress());
        order.setOrderState("1");//未接单


        String oldIds = "";
        for(Map<String, Object> a:list){//解析预定产品，并保存
            String oldId = String.valueOf(a.get("id"));
            String attributeId = String.valueOf(a.get("attributeId"));
            String partsId = String.valueOf(a.get("partsId"));
            String count = String.valueOf(a.get("orderCount"));
            BigDecimal price = new BigDecimal(String.valueOf(a.get("orderPrice")));

            ZsyhStoreOrderRel rel = ZsyhStoreOrderRel.getInstance();
            rel.setId(IdWorker.getId());
            rel.setOrderId(order.getId());
            rel.setPartsId(partsId);
            rel.setAttributeId(attributeId);
            rel.setOrderCount(count);
            rel.setOrderPrice(price);
            rel.setCreateTime(date);
            rel.setIsPlatform("1");
            relList.add(rel);

            oldIds += oldId + ",";

        }
        zsyhStoreOrderRelService.deleteZsyhStoreOrderRelByIds(oldIds);
        zsyhStoreOrderRelService.insertZsyhStoreOrderRelBatch(relList);

//        List<ZsyhStoreOrder> orderList = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
            zsyhStoreOrderService.insertZsyhStoreOrderApplet(order);

        return success("操作成功");

    }


    /**
     * 加入预订单 批量
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/saveZsyhStoreOrderRel")
    @ResponseBody
    @Transactional
    public AjaxResult saveZsyhStoreOrderRel(String userId, String partsJson){

        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(partsJson, new TypeReference<List<Map<String, Object>>>() {});

        for(Map<String, Object> a:list){//解析预定产品，并保存
            String partsId = String.valueOf(a.get("partsId"));
            String attributeId = String.valueOf(a.get("attributeId"));
            String orderCount = String.valueOf(a.get("orderCount"));
            BigDecimal orderPrice = new BigDecimal(String.valueOf(a.get("orderPrice")));

            ZsyhStoreOrderRel rel = ZsyhStoreOrderRel.getInstance();
            rel.setId(IdWorker.getId());
            rel.setOrderId(userId);
            rel.setPartsId(partsId);
            rel.setAttributeId(attributeId);
            rel.setOrderPrice(orderPrice);
            rel.setIsPlatform("1");
            rel.setOrderCount(orderCount);

            ZsyhStoreOrderRel orderRel = ZsyhStoreOrderRel.getInstance();
            orderRel.setOrderId(userId);
            orderRel.setAttributeId(attributeId);
            List<ZsyhStoreOrderRel> list1 = zsyhStoreOrderRelService.selectZsyhStoreOrderRelList(orderRel);
            if(list1.size() > 0){
                rel.setId(list1.get(0).getId());
                String oldCount = list1.get(0).getOrderCount();
                if(EmptyUtils.isNotEmpty(oldCount)){
                    double _orderCount = Double.valueOf(list1.get(0).getOrderCount());
                    _orderCount += Double.valueOf(orderCount);
                    rel.setOrderCount(String.valueOf(_orderCount));
                }
                zsyhStoreOrderRelService.updateZsyhStoreOrderRel(rel);
            }else{
                rel.setCreateTime(new Date());
                zsyhStoreOrderRelService.insertZsyhStoreOrderRel(rel);
            }
        }

        return success("操作成功");

    }
    /**
     * 加入预订单  -- 废弃
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/saveZsyhStoreOrderRelOld")
    @ResponseBody
    @Transactional
    public AjaxResult saveZsyhStoreOrderRelOld(String userId, String partsId, String attributeId, String orderCount, String orderPrice){
        ZsyhStoreOrderRel rel = ZsyhStoreOrderRel.getInstance();
        rel.setId(IdWorker.getId());
        rel.setOrderId(userId);
        rel.setPartsId(partsId);
        rel.setAttributeId(attributeId);
        rel.setOrderPrice(new BigDecimal(orderPrice));
        rel.setIsPlatform("1");
        rel.setOrderCount(orderCount);

        ZsyhStoreOrderRel orderRel = ZsyhStoreOrderRel.getInstance();
        orderRel.setOrderId(userId);
        orderRel.setAttributeId(attributeId);
        List<ZsyhStoreOrderRel> list = zsyhStoreOrderRelService.selectZsyhStoreOrderRelList(orderRel);
        if(list.size() > 0){
            rel.setId(list.get(0).getId());
            String oldCount = list.get(0).getOrderCount();
            if(EmptyUtils.isNotEmpty(oldCount)){
                double _orderCount = Double.valueOf(list.get(0).getOrderCount());
                _orderCount += Double.valueOf(orderCount);
                rel.setOrderCount(String.valueOf(_orderCount));
            }
            zsyhStoreOrderRelService.updateZsyhStoreOrderRel(rel);
        }else{
            rel.setCreateTime(new Date());
            zsyhStoreOrderRelService.insertZsyhStoreOrderRel(rel);
        }
        return success("操作成功");
    }

    /**
     * 获取预定单列表
     * @param userId
     * @return
     */
    @RequestMapping("/getZsyhStoreOrderRelByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getZsyhStoreOrderRelByUserId(String userId){
        if(EmptyUtils.isEmpty(userId)){
            return error("用户id不允许为空");
        }
        ZsyhStoreOrderRel orderRel = ZsyhStoreOrderRel.getInstance();
        orderRel.setOrderId(userId);
        List<ZsyhStoreOrderRel> list = zsyhStoreOrderRelService.selectZsyhStoreOrderRelList(orderRel);
        return success("操作成功", list);
    }

    /**
     * 删除预订单 单个商品
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/delStoreOrderRel")
    @ResponseBody
    @Transactional
    public AjaxResult delStoreOrderRel(String id){
        zsyhStoreOrderRelService.deleteZsyhStoreOrderRelById(id);
        return success("操作成功");
    }

    /**
     * 获取店铺订单数量 配件店 未结单
     * @param userId
     * @return
     */
    @RequestMapping("/getStorePartsOrderNum")
    @ResponseBody
    @Transactional
    public AjaxResult getStorePartsOrderNum(String userId){
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();

        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setPartsStoreId(repairId);
        order.setOrderState("1");
        List<ZsyhStoreOrder> list = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
        return success("成功", list.size());

    }


    /**
     * 师傅查看自己的预订单列表
     * @param userId
     * @return
     */
    @RequestMapping("/getStorePartsOrderList")
    @ResponseBody
    @Transactional
    public AjaxResult getStorePartsOrderList(String userId, String orderState, String pageNum, String pageSize){
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();

        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setStoreId(repairId);
        order.setOrderState(orderState);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhStoreOrder> list = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
        for(ZsyhStoreOrder storeOrder:list){
            String id = storeOrder.getId();
            ZsyhStoreOrderRel rel = new ZsyhStoreOrderRel();
            rel.setOrderId(id);
            List<ZsyhStoreOrderRel> relList = zsyhStoreOrderRelService.selectZsyhStoreOrderRelList(rel);
            storeOrder.setZsyhStoreOrderRelList(relList);
        }
        return success("成功", list);

    }


    /**
     * 配件店 店铺订单详情
     * @return
     */
    @RequestMapping("/getStorePartsOrderDetails")
    @ResponseBody
    @Transactional
    public AjaxResult getStorePartsOrderDetails(String orderId){
        ZsyhStoreOrder order = zsyhStoreOrderService.selectZsyhStoreOrderById(orderId);
        return success("成功", order);
    }

    /**
     * 接单
     * @param orderId
     * @return
     */
    @RequestMapping("/receiveStorePartsOrder")
    @ResponseBody
    @Transactional
    public AjaxResult receiveStorePartsOrder(String orderId){
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setId(orderId);
        order.setOrderState("2");
        zsyhStoreOrderService.updateZsyhStoreOrderApplet(order);
        return success("成功");
    }

    /**
     * 发货
     * @param orderId
     * @return
     */
    @RequestMapping("/deliverStorePartsOrder")
    @ResponseBody
    @Transactional
    public AjaxResult deliverStorePartsOrder(String orderId){
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setId(orderId);
        order.setOrderState("3");
        zsyhStoreOrderService.updateZsyhStoreOrderApplet(order);
        return success("成功");
    }


    /**
     * 获取店铺产品的规格列表
     * @return
     */
    @RequestMapping("/getStorePartsAttributeList")
    @ResponseBody
    public AjaxResult getStorePartsAttributeList(String partsId){
        ZsyhStoreAttributeself attributeself = new ZsyhStoreAttributeself();
        attributeself.setPartsId(partsId);
        List<ZsyhStoreAttributeself> list = zsyhStoreAttributeselfService.selectZsyhStoreAttributeselfList(attributeself);
        return AjaxResult.success("获取成功", list);
    }



    /**
     * 店铺自有产品 新增、修改
     * @param parts
     * @return
     */
    @RequestMapping("/updateStoreParts")
    @ResponseBody
    @Transactional
    public AjaxResult updateStoreParts(@RequestBody ZsyhStorePartsself parts){

        if(EmptyUtils.isNotEmpty(parts.getId())){
            zsyhStorePartsselfService.updateZsyhStorePartsself(parts);
        }else{
            ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(parts.getUserId());
            String repairId = store.getId();

            parts.setId(IdWorker.getId());
            parts.setStoreId(repairId);
            zsyhStorePartsselfService.insertZsyhStorePartsself(parts);
        }

        return AjaxResult.success("操作成功");
    }

    /**
     * 店铺自有产品的规格 新增、修改
     * @param attribute
     * @return
     */
    @RequestMapping("/updateStorePartsAttribute")
    @ResponseBody
    @Transactional
    public AjaxResult updateStorePartsAttribute(@RequestBody ZsyhStoreAttributeself attribute){

        if(EmptyUtils.isNotEmpty(attribute.getId())){
            zsyhStoreAttributeselfService.updateZsyhStoreAttributeself(attribute);
        }else{
            attribute.setId(IdWorker.getId());
            zsyhStoreAttributeselfService.insertZsyhStoreAttributeself(attribute);
        }
        return AjaxResult.success("操作成功");
    }


    /**
     * 店铺自有产品 删除
     * @return
     */
    @RequestMapping("/delStoreParts")
    @ResponseBody
    @Transactional
    public AjaxResult delStoreParts(String id){
        ZsyhStoreAttributeself attributeself = new ZsyhStoreAttributeself();
        attributeself.setPartsId(id);
        List<ZsyhStoreAttributeself> list = zsyhStoreAttributeselfService.selectZsyhStoreAttributeselfList(attributeself);
        if(list.size() > 0){
            return AjaxResult.error("产品下有规格，不允许删除");
        }
        zsyhStorePartsselfService.deleteZsyhStorePartsselfById(id);
        return AjaxResult.success("操作成功");
    }

    /**
     * 删除店铺自由产品的规格
     * @return
     */
    @RequestMapping("/delStorePartsAttribute")
    @ResponseBody
    @Transactional
    public AjaxResult delStorePartsAttribute(String id){
        zsyhStoreAttributeselfService.deleteZsyhStoreAttributeselfById(id);
        return AjaxResult.success("操作成功");
    }


    /**
     * 个人中心 获取产品预订单列表
     * @param userId
     * @return
     */
    @RequestMapping("/getStorePartsOrderListByUserId")
    @ResponseBody
    @Transactional
    public AjaxResult getStorePartsOrderListByUserId(String userId, String orderState, String pageNum, String pageSize){
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();

        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setStoreId(repairId);
        order.setOrderState(orderState);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhStoreOrder> list = zsyhStoreOrderService.selectZsyhStoreOrderList(order);



        return success("成功", list);

    }

    /**
     * 取消店铺预订单
     * @param id
     * @return
     */
    @RequestMapping("/cancelStorePartsOrder")
    @ResponseBody
    @Transactional
    public AjaxResult cancelStorePartsOrder(String id){

        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setId(id);
        order.setOrderState("0");
        zsyhStoreOrderService.updateZsyhStoreOrderApplet(order);
        return success("成功");

    }


    /**
     * 会长获取我的店铺订单列表
     * @return
     */
    @RequestMapping("/getStorePartsOrderListByAgentId")
    @ResponseBody
    @Transactional
    public AjaxResult getStorePartsOrderListByAgentId(String agentId, String orderState, String pageNum, String pageSize){
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhStoreOrder order = new ZsyhStoreOrder();
        order.setAgentId(agentId);
        order.setOrderState(orderState);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhStoreOrder> list = zsyhStoreOrderService.selectZsyhStoreOrderList(order);
        return success("成功", list);

    }
}
